package com.imcreator.icchat.ServerThreads;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.imcreator.icchat.Colors;
import com.imcreator.icchat.IcChat;
import com.imcreator.icchat.Methods;

public class AcceptThread extends Thread {
    public ServerSocket serverSocket;
    public Boolean running = true;

    public AcceptThread(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public void run() {
        while (running) {
            try { // 接受连接
                Socket socket = serverSocket.accept();
                if (!running) {
                    Methods.sendMessage(socket, "unaccepted", "shutdown");
                    continue;
                }
                Methods.printAbove(Methods.changeColor(
                    "客户端(" + socket.getInetAddress() + ")已连接，开始验证", 
                    Colors.YELLOW_BOLD));
                ClientThread clientThread = new ClientThread(socket);
                IcChat.verifingList.add(clientThread);
                clientThread.start();
            } catch (IOException e) {
                if (running) {
                    Methods.printAbove(Methods.changeColor(
                        "连接客户端时出错", 
                        Colors.RED_BOLD));
                    e.printStackTrace();
                }
            }
        }
    }
}
